//
//  EmoticonKeyBoardView.swift
//  SinaWebo
//
//  Created by mis on 16/3/24.
//  Copyright © 2016年 mis. All rights reserved.
//

import UIKit

private let EmoticonCellId = "EmoticonCellId"
private let toolBarHeight: CGFloat = 36
class EmoticonKeyBoardView: UIView {
    
    var selectEmltion: ((em: Emoticon) -> ())?
    private lazy var packages = EmoticonManager.emoticonManager.packages
    
    @objc private func itemClick(item: UIBarButtonItem) {
        let indexPath = NSIndexPath(forItem: 0, inSection: item.tag)
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: true)
    }

    init(selectEmltion: ((em: Emoticon) -> ())) {
        self.selectEmltion = selectEmltion
        let rect = CGRect(x: 0, y: 0, width: screenW, height: 220)
        super.init(frame: rect)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var toolBar: UIToolbar = UIToolbar()
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        let w = UIScreen.mainScreen().bounds.width / 7
        layout.itemSize = CGSize(width: w, height: w)
        let margin = (self.bounds.height - 3 * w - toolBarHeight) / 4
        layout.sectionInset = UIEdgeInsets(top: margin, left: 0, bottom: margin, right: 0)
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.whiteColor()
        cv.registerClass(EmoticonCell.self, forCellWithReuseIdentifier: EmoticonCellId)
        cv.delegate = self
        cv.dataSource = self
        cv.pagingEnabled = true
        return cv
    }()
    
}

extension EmoticonKeyBoardView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].emoticonList.count
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCellId, forIndexPath: indexPath) as! EmoticonCell
        cell.emoticon = packages[indexPath.section].emoticonList[indexPath.item]
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let em = packages[indexPath.section].emoticonList[indexPath.item]
        selectEmltion?(em: em)
    }
}

extension EmoticonKeyBoardView {
    private func setupUI() {
        addSubview(toolBar)
        addSubview(collectionView)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(toolBarHeight)
        }
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        setupToolBar()
    }
    
    private func setupToolBar() {
        toolBar.tintColor = UIColor.lightGrayColor()
        var index = 0
        var items = [UIBarButtonItem]()
        for package in packages {
            let item = UIBarButtonItem(title: package.group_name_cn, style: .Plain, target: self, action: "itemClick:")
            items.append(item)
            items.last?.tag = index++
            let spare = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(spare)
        }
        items.removeLast()
        toolBar.items = items
    }
}
